package com.njh.mian.net;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.njh.kaleidoscope.common.log.LogUtils;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class Net {
    public static final String HOST = "http://192.168.2.47:8080/ssyt-user";
    public static final String BASE_API = HOST + "/";
    public static final String HOST1 = "";
    public static final String BASE_API1 = HOST1 + "/";
    private static Retrofit retrofit;
    private static Retrofit retrofit1;
    private static Retrofit retrofit2;
    private static ApiService apiService, apiService1,apiService2;

    private static ApiService apiService_no_token;

    public static void init(final Context context) {
        //  if(!isInit) {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        //HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        //设置日志级别,最高
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                LogUtils.e("interceptor", message);
                System.out.println(message);
            }
        });
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_API)
                .client(new OkHttpClient.Builder()
                        .connectTimeout(10, TimeUnit.SECONDS)
                        .addInterceptor(interceptor)
                       .addInterceptor(new Interceptor() {
                           @Override
                           public Response intercept(Chain chain) throws IOException {
//                               String token = SPUserInfo.getToken(context);
                               String token ="";
                               Request newRequest = chain.request()
                                       .newBuilder()
                                       .addHeader("token", token)
                                       .addHeader("platform", "1")
                                       .build();
                               return chain.proceed(newRequest);
                           }
                       })
                        .build())

                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();

        retrofit1 = new Retrofit.Builder()
                .baseUrl(BASE_API)
                .client(new OkHttpClient.Builder()
                        .connectTimeout(10, TimeUnit.SECONDS)
                        .addInterceptor(interceptor)

                        .build())

                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        retrofit2 = new Retrofit.Builder()
                .baseUrl("http://192.168.2.47:8080/ssyt-app/")
                .client(new OkHttpClient.Builder()
                        .connectTimeout(10, TimeUnit.SECONDS)
                        .addInterceptor(interceptor)
                        .addInterceptor(new Interceptor() {
                            @Override
                            public Response intercept(Chain chain) throws IOException {
//                                String token = SPUserInfo.getToken(context);
                                String token ="";
                                Request newRequest = chain.request()
                                        .newBuilder()
                                        .addHeader("token", token)
                                        .addHeader("platform", "1")
                                        .build();
                                return chain.proceed(newRequest);
                            }
                        })
                        .build())

                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();

    }
/*  public Net() {
    if (retrofit == null) {
      HttpLoggingInterceptor log = new HttpLoggingInterceptor();
      log.setLevel(HttpLoggingInterceptor.Level.BODY);
      retrofit = new Retrofit.Builder()
          .baseUrl(BASE_API)
          .client(new OkHttpClient.Builder()
              .connectTimeout(10, TimeUnit.SECONDS)
              .addInterceptor(log)

              .build())
          .addConverterFactory(GsonConverterFactory.create())
          .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
          .build();
    }
  }*/

    public static ApiService getService1() {

        if (apiService1 == null) {
            apiService1 = retrofit1.create(ApiService.class);
        }
        return apiService1;
    }

    public static ApiService getService() {

        if (apiService == null) {
            apiService = retrofit.create(ApiService.class);
        }
        return apiService;
    }
    public static ApiService getService2() {

        if (apiService2 == null) {
            apiService2 = retrofit2.create(ApiService.class);
        }
        return apiService2;
    }
    public ApiService service() {
        return retrofit.create(ApiService.class);
    }
}
